package com.flmes.base.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.experimental.UtilityClass;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;

/**
 * @Author: flmes
 * @Date: 2024/10/8 20:20
 */
@UtilityClass
public class JwtUtil {

    /**
     * 生成jwt
     * @param key
     * @param params
     * @param expirationByHour 有效期,小时
     * @return
     */
    public String getJwt(SecretKey key, Map<String, Object> params, long expirationByHour) {
        return Jwts.builder()
                .claims(params)
                .signWith(key)
                .expiration(new Date(System.currentTimeMillis() + expirationByHour * 3600 * 1000))
                .compact();
    }

    public Claims parseJwt(SecretKey key, String jwt) {
        return Jwts.parser().verifyWith(key).build().parseSignedClaims(jwt).getPayload();
    }

}
